
#include <asm.h>

.global C_SYMBOL(RECT888To565FLIPWMMX)

C_SYMBOL(RECT888To565FLIPWMMX):
//.long 0xe7fddefe

    MOV     R12, SP
    STMFD	SP!, {R4-R12,LR}
Start:

    LDR R11, [R0, #0x0]                 //pSrc
    LDR	R8,  [R0, #0xC]                 //SrcStep
    LDR	R10, [R1, #0x0]                 //pDst
	LDR	R12, [R1, #0xC]                 //DstStep

    LDR	R4, [R0, #0x4]                  //width
	LDR	R7, [R0, #0x8]                  //height
//.long 0xe7fddefe
	MOV LR, R4, LSL #2                  // LR = src pitch

    MOV     R5, #0

    LDR     R0, = 0x00000002
    TMCR    wCGR0, R0
    LDR     R0, = 0x00000003
    TMCR    wCGR1, R0
    LDR     R0, = 0x00000005
    TMCR    wCGR2, R0
    LDR     R0, = 0x0000000B
    TMCRR   wR13, R0, R5

LoopLine:
	MOV	R3, LR                          // R3 = src pitch
	MOV	R2, R10
	MOV	R0, R11
	B Alpha_8u_C4ARow

Alpha_8u_C4ARow:

    AND	R6, R2,	#7
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    WLDRW   wR4, [R0], #4
    CMP	R6, R3, LSR #1

    MOVGT	R6, R3, LSR #1
    SUB	R3, R3,	R6, LSL #1

_Loop_One_:

    WUNPCKELUB wR5, wR4
    WUNPCKELUB wR4, wR4
    WSRLHG  wR4, wR4, wCGR0
    WSRLHG  wR5, wR5, wCGR1
    TEXTRMUH R5, wR5, #0
    TEXTRMUH R4, wR4, #1
    TEXTRMUH R9, wR5, #2
    SUBS	R6, R6,	#2
    WLDRWGT   wR4, [R0], #4
    ORR     R5, R5, R4, LSL#5
    ORR     R5, R5, R9, LSL#11

    STRH    R5, [R2, #-2]!

    BGT	_Loop_One_

    CMP	R3, #0
    BEQ     Next

_Aligned8_:
    SUBS	R3, R3,	#0x20
    ADDMI	R6, R3,	#0x20
    ADDMI	R3, R3,	#0x20
    BMI	_Process_One_
    AND	R4, R0,	#7
    CMP R4, #0
    BNE	_UnAligned8_

    WLDRD   wR0, [R0], #8       @pData
    WLDRD   wR1, [R0], #8       @pData

    WLDRD   wR9, [R0], #8       @pData
    WLDRD   wR10, [R0], #8       @pData

_Loop_Aligned8_:

    SUBS	R3, R3,	#0x20
    PLD	[R0,#0x40]

    //WUNPCKILW wR2, wR0, wR1
    //WUNPCKIHW wR3, wR0, wR1

    WUNPCKIHW wR2, wR1, wR0
    WUNPCKILW wR3, wR1, wR0

    WUNPCKILB wR0, wR2, wR3
    WUNPCKIHB wR1, wR2, wR3
    WUNPCKILH wR2, wR0, wR1
    WUNPCKIHH wR3, wR0, wR1

    WUNPCKELUB wR6, wR2     @B
    WUNPCKEHUB wR7, wR2     @G
    WUNPCKELUB wR8, wR3     @R

    WLDRDGE  wR0, [R0], #8

    WSRLHG  wR6, wR6, wCGR1
    WSRLHG  wR7, wR7, wCGR0
    WSRLHG  wR8, wR8, wCGR1

    WLDRDGE  wR1, [R0], #8

    WSLLHG  wR7, wR7, wCGR2
    WSLLH  wR8, wR8, wR13

    WOR     wR6, wR6, wR7
    WOR     wR6, wR6, wR8

    WSTRD   wR6, [R2, #-8]!

    //WUNPCKILW wR2, wR9, wR10
    //WUNPCKIHW wR3, wR9, wR10

    WUNPCKIHW wR2, wR10, wR9
    WUNPCKILW wR3, wR10, wR9

    WUNPCKILB wR9, wR2, wR3
    WUNPCKIHB wR10, wR2, wR3
    WUNPCKILH wR2, wR9, wR10
    WUNPCKIHH wR3, wR9, wR10

    WLDRDGE  wR9, [R0], #8

    WUNPCKELUB wR6, wR2     @B
    WUNPCKEHUB wR7, wR2     @G
    WUNPCKELUB wR8, wR3     @R

    WLDRDGE  wR10, [R0], #8

    WSRLHG  wR6, wR6, wCGR1
    WSRLHG  wR7, wR7, wCGR0
    WSRLHG  wR8, wR8, wCGR1

    WSLLHG  wR7, wR7, wCGR2
    WSLLH  wR8, wR8, wR13

    WOR     wR6, wR6, wR7
    WOR     wR6, wR6, wR8

    WSTRD   wR6, [R2, #-8]!

    BGE	_Loop_Aligned8_
    ADDS	R3, R3,	#0x20
    BEQ     Next
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8
    BGT	_Process_One_



_UnAligned8_:

    BIC	R0, R0,	#7
    TMCR    wCGR3, R4

    WLDRD   wR0, [R0], #8       @pData
    WLDRD   wR1, [R0], #8       @pData

    WLDRD   wR9, [R0], #8       @pData
    WLDRD   wR10, [R0], #8       @pData

_Loop_UnAligned8_:

    TST	R4, #7
    WLDRDNE wR12, [R0]

    SUBS	R3, R3,	#0x20
    PLD	[R0,#0x40]

    WALIGNR3 wR0 ,wR0, wR1
    WALIGNR3 wR1 ,wR1, wR9

    //WUNPCKILW wR2, wR0, wR1
    //WUNPCKIHW wR3, wR0, wR1

    WUNPCKIHW wR2, wR1, wR0
    WUNPCKILW wR3, wR1, wR0

    WUNPCKILB wR0, wR2, wR3
    WUNPCKIHB wR1, wR2, wR3
    WUNPCKILH wR2, wR0, wR1
    WUNPCKIHH wR3, wR0, wR1

    WUNPCKELUB wR6, wR2     @B
    WUNPCKEHUB wR7, wR2     @G
    WUNPCKELUB wR8, wR3     @R

    WLDRDGE  wR0, [R0], #8

    WSRLHG  wR6, wR6, wCGR1
    WSRLHG  wR7, wR7, wCGR0
    WSRLHG  wR8, wR8, wCGR1

    WLDRDGE  wR1, [R0], #8

    WSLLHG  wR7, wR7, wCGR2
    WSLLH  wR8, wR8, wR13

    WOR     wR6, wR6, wR7
    WOR     wR6, wR6, wR8

    WSTRD   wR6, [R2, #-8]!

    WALIGNR3 wR9, wR9, wR10
    WALIGNR3 wR10, wR10, wR12

    //WUNPCKILW wR2, wR9, wR10
    //WUNPCKIHW wR3, wR9, wR10

    WUNPCKIHW wR2, wR10, wR9
    WUNPCKILW wR3, wR10, wR9

    WUNPCKILB wR9, wR2, wR3
    WUNPCKIHB wR10, wR2, wR3
    WUNPCKILH wR2, wR9, wR10
    WUNPCKIHH wR3, wR9, wR10

    WLDRDGE  wR9, [R0], #8

    WUNPCKELUB wR6, wR2     @B
    WUNPCKEHUB wR7, wR2     @G
    WUNPCKELUB wR8, wR3     @R

    WLDRDGE  wR10, [R0], #8

    WSRLHG  wR6, wR6, wCGR1
    WSRLHG  wR7, wR7, wCGR0
    WSRLHG  wR8, wR8, wCGR1

    WSLLHG  wR7, wR7, wCGR2
    WSLLH  wR8, wR8, wR13

    WOR     wR6, wR6, wR7
    WOR     wR6, wR6, wR8

    WSTRD   wR6, [R2, #-8]!

    BGE	_Loop_UnAligned8_

    ADD	R0, R0,	R4
    ADDS	R3, R3,	#0x20
    BEQ     Next
    MOVGT	R6, R3
    BGT	_Process_One_


Next:
	SUB	R7, R7,	#1
	ADD	R11, R11, R8
	SUB	R10, R10, R12
	CMP	R7, #0
	BHI	LoopLine

End:

    LDMFD   SP, {R4-R11,SP,PC}
//	LDMFD	SP!, {R4-R12,PC}

